//
//  ECGFiltersLib.m
//  CPETvhECGService
//
//  Created by moon_zm on 2024/9/12.
//

#import "ECGFiltersLib.h"
#import <vhStressTemplLib/vhStressTemplLib-Swift.h>

@interface ECGFiltersLib ()
@property (nonatomic, strong) vhFiltersLib *vhFilterLib;//心电滤波对象

@property (nonatomic, assign) int ecgRate;//采样率
@property (nonatomic, assign) int freq_low;//低通滤波
@property (nonatomic, assign) int freq_notch;//工频滤波
@property (nonatomic, assign) int smoothStrength;//肌电滤波

@end

@implementation ECGFiltersLib

+ (instancetype)shared {
    static ECGFiltersLib *identifier = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        identifier = [[ECGFiltersLib alloc] init];
    });
    return identifier;;
}

- (instancetype)init {
    if (self = [super init]) {
        self.ecgRate = 500;
        self.freq_low = 0;
        self.freq_notch = 0;
        self.smoothStrength = 2;
        //创建滤波器
        self.vhFilterLib = [[vhFiltersLib alloc] initWithMe:@"hspecg@163.com" fs:self.ecgRate chn:12 freqLpass:self.freq_low freqNotch:self.freq_notch smoothStrength:self.smoothStrength];
    }
    return self;
}

///设置滤波器的采样率
- (void)setFilterWithEcgRate:(int)ecgRate {
    self.ecgRate = ecgRate;
    [self updateVhFilterLibData];
}

/// 设置滤波器的滤波参数
- (void)setFilterFreqLow:(kECGFreqLowType)freqLow {
    int freqLowTmp = 50;
    switch (freqLow) {
        case kLowType_40:
            freqLowTmp = 40;
            break;
        case kLowType_70:
            freqLowTmp = 70;
            break;
        case kLowType_100:
            freqLowTmp = 100;
            break;
        case kLowType_OFF:
            freqLowTmp = 0;
            break;
        default:
            break;
    }
    if (self.freq_low == freqLowTmp) {
        return;
    }
    self.freq_low = freqLowTmp;
}
- (void)setFilterFreqNotch:(kECGFreqNotchType)freqNotch {
    int freqNotchTmp = 50;
    switch (freqNotch) {
        case kNotchType_50:
            freqNotchTmp = 50;
            break;
        case kNotchType_60:
            freqNotchTmp = 60;
            break;
        case kNotchType_OFF:
            freqNotchTmp = 0;
            break;
        default:
            break;
    }
    if (self.freq_notch == freqNotchTmp) {
        return;
    }
    self.freq_notch = freqNotchTmp;
}
- (void)setFilterFreqMooth:(kECGSmoothType)freqMooth {
    int freqMoothTmp = 2;
    switch (freqMooth) {
        case kSmoothType_OFF:
            freqMoothTmp = 0;
            break;
        case kSmoothType_weak:
            freqMoothTmp = 1;
            break;
        case kSmoothType_strong:
            freqMoothTmp = 2;
            break;
        default:
            break;
    }
    if (self.smoothStrength == freqMoothTmp) {
        return;
    }
    self.smoothStrength = freqMoothTmp;
}

- (void)updateVhFilterLibData {

    BOOL ok = [self.vhFilterLib updateWithFs:self.ecgRate freqLpass:self.freq_low freqNotch:self.freq_notch smoothStrength:self.smoothStrength];
    if(!ok){
        NSLog(@"update ecg filter error");
    }
//    
//    [self.vhFilterLib switchFiltersOnOffLpass:(self.freq_low == 0 ? NO : YES) onOffNotch:(self.freq_notch == 0 ? NO : YES) onOffSmooth:(self.smoothStrength == 0 ? NO : YES)];
}

- (NSArray<NSNumber *> * _Nullable)filtersWithData:(NSArray<NSNumber *> * _Nonnull)data {
    return [self.vhFilterLib filtersWithData:data];
}
@end

